package com.cloud.jdkTest;

import java.util.Optional;

/**
 * Optional测试
 */
public class OptionalTest {

    private static class Outer {
        Nested nested;
        Nested getNested() {
            return nested;
        }
    }

    private static class Nested {
        Inner inner;
        Inner getInner() {
            return inner;
        }
    }

    private static class Inner {
        String foo;
        String getFoo() {
            return foo;
        }
    }


    public static void main(String[] args) {

        Optional<String> name = Optional.of("Tony");
        if (name.isPresent()) {
            System.out.println(name.get());
        }

        Optional<String> upperName = name.map( value -> value.toUpperCase() );
        System.out.println(upperName.orElse("No value found"));

        Optional<String> longName = name.filter( value -> value.length() > 12 );
        System.out.println(longName.orElse("There is no long name"));


        Optional<Outer> outerOp = Optional.of(new Outer());
        Boolean isPresent = outerOp.map( value -> value.getNested())
                .map( value -> value.getInner())
                .map( value -> value.getFoo())
                .isPresent();

        System.out.println(isPresent);
    }
}


